package com.dev.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.dev.security.entity.Log;
import com.dev.security.entity.Menu;
import com.dev.security.entity.Role;
import com.dev.security.entity.RoleMenu;
import com.dev.security.mapper.LogMapper;
import com.dev.security.mapper.RoleMapper;
import com.dev.security.mapper.RoleMenuMapper;
import com.dev.security.service.LogService;
import com.dev.security.service.RoleService;
import com.dev.security.vo.LogVo;
import com.dev.security.vo.MenuVo;
import com.dev.security.vo.PageVo;
import com.dev.security.vo.RoleVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 日志Service实现
 *
 * @author yuboon
 * @version v1.0
 * @date 2020/04/04
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    @Transactional
    public int save(Log log){
        log.setCreateDatetime(new Date());
        return logMapper.insert(log);
    }

    @Override
    public PageVo<LogVo> selectList(LogVo logVo,PageVo<LogVo> pageVo) {
        Page page = PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<Log> logList = logMapper.selectList(logVo);
        List<LogVo> logVoList = CollectionUtil.newArrayList();
        for(Log log : logList){
            LogVo vo = new LogVo();
            BeanUtil.copyProperties(log,vo);
            vo.setCreateDatetime(DateUtil.formatDateTime(log.getCreateDatetime()));
            logVoList.add(vo);
        }
        pageVo.setList(logVoList);
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }
}
